 <!--
* description: 其它出库
* author: wangjuan
* createdDate: 2021-4-13
-->
<template>
  <div class="app-container app-container-table">
    <one-table-template
      ref="multipleTable"
      :dynamic-buttons="tableButtons"
      :dynamic-components="tableComponents"
      :dynamic-table-cols="tableCols"
      :dynamic-api-config="apiConfig"
      :dynamic-form-fields="formField"
      :dynamic-is-show-select="true"
      :dynamic-is-show-more-btn="true"
      :callback-obj="callbackObj"
    />
  </div>
</template>
<script>
import { oneTableWithViewTemplateMixins } from '@/components/mixins/oneTableWithViewTemplateMixins'
import OneTableTemplate from '@/components/templates/oneTable'
import api from '@/api/pa/apiBranchWarehouse'
import Vue from 'vue'

export default {
  name: 'Otherdeliveryquery',

  components: {
    OneTableTemplate
  },

  // 组件混入对象：{data[listQuery] methods[queryTable]}
  mixins: [oneTableWithViewTemplateMixins],

  // 阻塞路由预加载网格中组件的数据
  beforeRouteEnter(to, from, next) {
    Vue.prototype.$ConfigCache.CacheConfig.initData([to.path, ''], function() {
      next()
    })
  },

  data() {
    // 页面标识（与上面beforeRouteEnter中的对应）
    const viewPageCode = this.$route.path

    // 绑定事件配置化事件
    this.$ConfigCache.CacheConfig.bindEvent(this, [viewPageCode])

    return {
      selectData: {},
      apiConfig: api.queryOtherOutboundInfo,

      // 动态组件-按钮
      tableButtons: [
        {
          compKey: 'btnKey1',
          type: 'primary',
          size: 'small',
          name: 'search',
          clickEvent: () => this.queryTable(1),
          text: this.$t('sys.button.query'), // 查询
          position: 'right',
          fuzzySearch: true
        },
        {
          compKey: 'btnKey2',
          size: 'small',
          clickEvent: () => this.delivery(),
          text: '出库',
          position: 'left',
          disabled: true
        },
        {
          compKey: 'btnKey3',
          size: 'small',
          name: 'reset',
          clickEvent: () => this.reset(),
          text: this.$t('sys.button.reset'),
          position: 'right' // 重置
        }
      ],
      // 动态组件-查询条件
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
            .tableComponents
          : [
            {
              compKey: 'compKey1',
              labelName: '申请单号：',
              codeField: 'applyOrderCode',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey2',
              labelName: '出库类型：',
              codeField: 'outboundType',
              component: () => import('@/components/org/LookupValue'),
              oFields: 'outboundType,outboundTypeCn',
              options: [
                { outboundType: '4', outboundTypeCn: '领用出库' },
                { outboundType: '6', outboundTypeCn: '报废出库' }
              ],
              type: 'dropdownList',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey3',
              labelName: '领用去向：',
              codeField: 'applyDept',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey4',
              labelName: '申请人：',
              codeField: 'applyPerson',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey5',
              labelName: '申请日期：',
              codeField: 'beginDate,endDate',
              component: () => import('@/components/org/datePicker/datePicker'),
              dateType: 'daterange',
              isMul: false,
              isMust: false
            },
            {
              compKey: 'compKey6',
              labelName: '领用仓库：',
              codeField: 'warehouseName',
              component: () => import('@/components/org/commonSelect'),
              oFields: 'warehouseName,warehouseName',
              customObj: {
                api: api.queryOutStorage,
                listQuery: {
                  warehouseName: ''
                },
                selectType: '1'
              },
              type: 'dropdownList',
              isMul: false,
              isMust: false
            },
            {
              compKey: 'compKey7',
              labelName: '备件编码：',
              codeField: 'partNo',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: false
            },
            {
              compKey: 'compKey8',
              labelName: '成本中心：',
              codeField: 'costCentre',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: false
            }
          ],
      // 动态生成网格列
      tableCols:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          : [
            {
              prop: 'isRetrospect',
              label: '是否追溯件',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'warehouseId',
              label: '仓库ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'dlrId',
              label: '网点ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'partId',
              label: '网点ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'updateControlId',
              label: '并发控制ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'applyOrderCode',
              label: '申请单号'
            },
            {
              prop: 'applyDate',
              label: '申请日期',
              width: 140
            },
            {
              prop: 'warehouseName',
              label: '领用仓库',
              width: 140
            },
            {
              prop: 'outboundTypeCn',
              label: '出库类型'
            },
            {
              prop: 'applyDept',
              label: '领用去向'
            },
            {
              prop: 'applyUse',
              label: '出库用途'
            },
            {
              prop: 'partNo',
              label: '备件编码',
              width: 140
            },
            {
              prop: 'partName',
              label: '备件名称'
            },
            {
              prop: 'storageLocation',
              label: '仓位编码',
              width: 140
            },
            {
              prop: 'theoryUsableStorage',
              label: '理论可用库存'
            },
            {
              prop: 'stayOutboundQty',
              label: '待出库数量'
            },
            {
              prop: 'outStoreQty',
              label: '出库数量',
              isComponent: true,
              comps: [
                {
                  compKey: 'compKey5' + this.$utils.generateId(),
                  codeField: 'outStoreQty',
                  component: () => import('@/components/org/commonInput'),
                  type: 'inputText'
                }
              ],
              width: 120
            },
            {
              prop: 'retrospectNo',
              label: '追溯件号',
              isComponent: true,
              comps: [
                {
                  compKey: 'compKey5' + this.$utils.generateId(),
                  codeField: 'retrospectNo',
                  component: () => import('@/components/org/commonInput'),
                  type: 'inputText'
                }
              ],
              width: 120
            },
            {
              prop: 'costCentre',
              label: '成本中心'
            },
            {
              prop: 'applyPerson',
              label: '申请人'
            },
            {
              prop: 'receivePerson',
              label: '收货人'
            },
            {
              prop: 'linkPhone',
              label: '联系电话',
              width: 120
            },
            {
              prop: 'receiveAddress',
              label: '收货地址',
              width: 140
            }
          ],

      // 表单数据
      formField: this.$utils.getFormField(this, {
        applyOrderCode: '',
        outboundType: '',
        applyDept: '',
        applyPerson: '',
        beginDate: '',
        endDate: '',
        warehouseName: '',
        costCentre: '',
        partNo: '',

        isRetrospect: '',
        warehouseId: '',
        dlrId: '',
        partId: '',
        updateControlId: ''
      }, this.$ConfigCache.CacheConfig.cacheData[viewPageCode]),

      callbackObj: {
        onCheckSelectChange: (selection) => {
          this.selectData = selection
          this.handleSelectionChange(selection)
        }
      }
    }
  },

  mounted() {
    // 列表查询
    this.queryTable(1)
  },

  methods: {
    // 出库
    delivery() {
      // debugger
      this.$confirm('确定要出库吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const queryObj = {
        // 保存需要传 type="mutation"
          type: 'mutation',
          apiConfig: api.otherOutboundStorage,
          // 条件/实体参数（变量），根据typeParam中的定义配置

          variables: {
            dataInfo: this.selectData
          }
        }
        // 转换了中台请求格式数据
        var paramD = this.$getParams(queryObj)
        this.$requestAPI(paramD).then((response) => {
          if (response.result === '1') {
            this.$message({
              message: response.msg
            })
          } else {
            this.$message({
              message: response.msg
            })
          }
          this.queryTable(1)
        })
      })
    },

    // 当选择项发生变化时会触发该事件
    handleSelectionChange(selection) {
      const btns = this.tableButtons.filter(o => o.disabled !== undefined)
      if (btns.length > 0) {
        const isCheck = selection && selection.length > 0
        btns.forEach((item) => {
          item.disabled = !isCheck
        })
      }
    }
  }
}
</script>
